<template>
    <div class="notice_content_item">
        <div class="times">{{ data.createTime }}</div>
        <van-badge v-if="switchIndex === 1" class="desic" :dot="data.readTag === 0" position="top-left">
            <div class="title">{{ data.title }}</div>
            <div class="desic_content" v-html="data.content"></div>
            <div class="buttons">
                <div class="handles" @click="look(data)">查看</div>
            </div>
        </van-badge>
        <van-badge v-if="switchIndex === 0" class="desic" :dot="data.handleStatus === 0" position="top-left">
            <div class="title">{{ data.title }}</div>
            <div class="desic_content" v-html="data.content"></div>
            <div class="buttons">
                <div class="handles" v-if="data.handleStatus === 0" @click="dispose(data.templateCode, data)">去处理</div>
                <div class="handles" v-if="data.handleStatus === 1" @click="disposeLook(data.templateCode, data)">去查看
                </div>
            </div>
        </van-badge>
    </div>
</template>

<script setup lang="ts">
import { defineProps, defineEmits } from 'vue'
import { useRouter } from 'vue-router';
import { readList, readHandle } from '@/api/operation/index'
import { showToast } from 'vant'
defineProps(['data', 'switchIndex'])
const emit = defineEmits(['read', 'look'])
const router = useRouter();
// 调用查看修改状态
const getreadMsg = async (type: string, data: any) => {
    if (type === '消息') {
        await readList({ codes: [data.code], type: 0 })
    } else {
        await readHandle({ code: data.code })
    }
}
// 过期提醒查看
const look = async (data: any) => {
    try {
        emit('look', data)
        if (data.readTag == 0) {
            await getreadMsg('消息', data)
            emit('read', 'readTag', data.code)
        }
    } catch (error) {

    }
}
// 去处理
const dispose = async (templateCode: string, data: any) => {
    // 商品借调处理
    // if (data.handleStatus == 0) {
    //     await getreadMsg('代办', data)
    //     emit('read', 'handleStatus', data.code)
    // }
    if (templateCode === '3') {
        const extend = JSON.parse(data?.extend)
        if (extend && extend.url) {
            router.push({
                path: '/secondmentGoodsDetails',
                query: urlParamsToObject(extend.url)
            })
        } else {
            showToast('当前链接不存在');
        }
    } else if (templateCode === '7') {
        // 变价接收
        router.push({
            path: '/receiveDetails/',
            query: {
                id: data.businessCode,
                status: '4'
            }
        })
    } else if (templateCode === '12') {
        // 无合同审批中
        router.push({
            path: '/approvalDetails',
            query: {
                id: data.productId,
                status: '0'
            }
        })
    } else {
        // 招商入库处理
        router.push({
            path: '/warehouseApproval/' + data.businessCode
        })
    }
}
// 去查看
const disposeLook = async (templateCode: string, data: any) => {
    // 商品借调处理
    // if (data.handleStatus == 0) {
    //     await getreadMsg('代办', data)
    //     emit('read', 'handleStatus', data.code)
    // }
    if (templateCode === '3') {
        const extend = JSON.parse(data?.extend)
        if (extend && extend.url) {
            router.push({
                path: '/secondmentGoodsDetails',
                query: { ...urlParamsToObject(extend.url), look: 'look' }
            })
        } else {
            showToast('当前链接不存在');
        }
    } else if (templateCode === '7') {
        // 变价接收
        router.push({
            path: '/receiveDetails/',
            query: {
                id: data.businessCode,
                status: '4',
                look: 'look'
            }
        })
    } else if (templateCode === '12') {
        // 无合同审批中
        router.push({
            path: '/approvalDetails',
            query: {
                id: data.productId,
                status: '0'
            }
        })
    } else {
        // 招商入库处理
        router.push({
            path: '/warehouseApproval/' + data.businessCode,
            query: {
                look: 'look'
            }
        })
    }
}
// 分割路由地址参数，返回对象
const urlParamsToObject = (url: string) => {
    const queryString = url.split('?')[1];
    const paramsArr = queryString.split('&');
    const paramObj = {};
    paramsArr.forEach(param => {
        const [key, value] = param.split('=');
        paramObj[key] = decodeURIComponent(value);
    });
    return paramObj;
}
</script>

<style lang="less" scoped>
.notice_content_item {
    width: 100%;
    box-sizing: border-box;

    .times {
        font-weight: 400;
        font-size: 14px;
        color: #8492AD;
        text-align: center;
        width: 100%;
        margin: 20px 0 10px;
        background: #F6FAFF;
    }

    .desic {
        width: 345px;
        min-height: 146px;
        background: #FFFFFF;
        border-radius: 5px;
        margin: auto;
        padding: 20px 15px;
        box-sizing: border-box;
        display: flex;
        flex-flow: column;

        .title {
            font-weight: 500;
            font-size: 16px;
            color: #1A1B1C;
            width: 100%;
            text-align: center;
            margin-bottom: 15px;
        }

        .desic_content {
            max-height: 300px;
            overflow-y: auto;
            font-weight: 400;
            font-size: 14px;
            color: #8492AD;
            width: 100%;
            margin-bottom: 15px;
        }

        .buttons {
            width: 100%;
            display: flex;
            align-items: center;
            justify-content: flex-end;

            .ignore {
                width: 74px;
                height: 30px;
                background: #EBF4FF;
                border-radius: 2px;
                font-weight: 400;
                font-size: 14px;
                color: #8492AD;
                text-align: center;
                line-height: 30px;
                box-sizing: border-box;
                margin-right: 10px;
            }

            .handles {
                width: 74px;
                height: 30px;
                border-radius: 2px;
                border: 1px solid #0094FF;
                font-weight: 400;
                font-size: 14px;
                color: #0094FF;
                text-align: center;
                line-height: 30px;
                box-sizing: border-box;
            }

            .handles_read {
                width: 74px;
                height: 30px;
                border-radius: 2px;
                font-weight: 400;
                font-size: 14px;
                color: #ccc;
                text-align: center;
                line-height: 30px;
                box-sizing: border-box;
            }

            .ignore {
                padding: 0 10px;
                box-sizing: border-box;
                height: 30px;
                background: #EBF4FF;
                border-radius: 2px;
                display: flex;
                align-items: center;
                justify-content: center;
                font-weight: 400;
                font-size: 14px;
                color: #8492AD;
                margin-right: 15px;
            }
        }
    }
}
</style>